#!/bin/bash
# usage: curl https://git.oschina.net/haide1014/linux-script-init/raw/master/language/python/install/CentOS7/main.sh | bash -



# 注：仅在CentoO7.3测试通过，在其他系统可能会遇到各种问题，请自行解决~




# 前置软件包1:
yum install -y wget gcc zlib-devel bzip2-devel openssl-devel ncurses-devel readline-devel soci-sqlite3-devel


# 下载
cd /usr/local/src
wget -c http://mirrors.sohu.com/python/3.6.4/Python-3.6.4.tgz
tar -zxf Python-3.6.4.tgz

# 清除历史安装，如果重复
[ -d "/usr/local/python3.6" ] && rm -rf /usr/local/python3.6

# 编译安装，增加多核编译效率 make -j
cd Python-3.6.4
./configure --prefix=/usr/local/python3.6 --with-ensurepip=install
make -j "$(nproc)" && make install

# 软链
ln -sv /usr/local/python3.6/bin/python3.6 /usr/bin
ln -sv /usr/local/python3.6/bin/pip3.6 /usr/bin


# 创建python用户
useradd py36admin ; echo 'py36admin:123456' | chpasswd

pip3.6 install virtualenv
# 不知道从什么时候，下面这个不会自动创建软链接了
ln -sv /usr/local/python3.6/bin/virtualenv /usr/bin/virtualenv3.6

# 2. 开始为python3创建,其它python版本同理
cd /home/py36admin
virtualenv3.6 -p /usr/local/python3.6/bin/python3 venv
# 创建后的虚拟环境目录为: /home/py36admin/venv




# 3. 设置py36登录自动激活virtualenv
# 默认为 PATH=$PATH:$HOME/.local/bin:$HOME/bin
cp /home/py36admin/.bash_profile /home/py36admin/.bash_profile.$(date +%Y%m%d_%H%M%S)
echo '# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi

# User specific environment and startup programs
source ~/venv/bin/activate
PATH=$PATH:$HOME/.local/bin:$HOME/bin:$HOME/venv/bin

export PATH
' > /home/py36admin/.bash_profile

# 创建编码目录
# 创建应用发布目录
[ ! -d "/home/py36admin/code" ] && mkdir -p /home/py36admin/code
[ ! -d "/home/py36admin/deploy" ] && mkdir -p /home/py36admin/deploy



# 不退出用户,手动执行加载
# source ~/.bash_profile

# 修改用户所有权
chown -R py36admin.py36admin /home/py36admin


# 设置root用户的
cd /root 
[ ! -d ".pip" ] && mkdir .pip
echo "\
[global]
index-url = https://pypi.doubanio.com/simple/

[install]
trusted-host=pypi.doubanio.com\
" > ./.pip/pip.conf

# 设置 py36admin 用户的
cd /home/py36admin
[ ! -d ".pip" ] && mkdir .pip
echo "\
[global]
index-url = https://pypi.doubanio.com/simple/

[install]
trusted-host=pypi.doubanio.com\
" > ./.pip/pip.conf


echo "\

-----------------------------------------------------------
|   python的专用开发用户                                  |
|   Linux用户名：py36admin                                |
|   密码: 123456                                          |
|   可以使用： su - py36admin 来一键切换到python环境      |
|   切换py36admin后，pip安装的软件，均在本用户的家目录下  |
-----------------------------------------------------------

"



